//
//  MovieDetailViewController.m
//  doubanMovie
//
//  Created by lanou3g on 15/11/20.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "MovieDetailViewController.h"
#import "MovieDetailView.h"

#import "MovieDetail.h"
#import "DataHandle.h"
#import "FileHandle.h"

#import "LoginViewController.h"
#import "DataBase.h"
#import "UserInformation.h"
@interface MovieDetailViewController ()<dataHandleDelegate>



@end

@implementation MovieDetailViewController

- (void)loadView
{
    self.movieDetailView = [[MovieDetailView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.view = self.movieDetailView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.movieDetail.movieName;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_back@2x.png" ] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemAction:)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_share@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonItemAction:)];
    
    self.movieDetailView.movieDetail = self.movieDetail;
    CGFloat height = [MovieDetailViewController heightForLabelText:self.movieDetail.actors];
    CGRect rect = self.movieDetailView.actorsLabel.frame;
    rect.size.height = height;
    self.movieDetailView.actorsLabel.frame = rect;
    
    CGRect rectMoviePlot_simple = self.movieDetailView.moviePlot_simpleLabel.frame;
    rectMoviePlot_simple.origin.y -= (100 - height);
    self.movieDetailView.moviePlot_simpleLabel.frame = rectMoviePlot_simple;
    
    CGFloat height1 = [MovieDetailViewController heightForLabelText:self.movieDetail.plot_simple];
    rect = self.movieDetailView.plot_simpleLabel.frame;
    rect.size.height = height1;
    rect.origin.y -= (100 - height);
    self.movieDetailView.plot_simpleLabel.frame = rect;
    self.movieDetailView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height + height + height1 - 200);

    
}

- (void)reLoad
{
    [self.view reloadInputViews];
}

- (void)rightBarButtonItemAction:(UIBarButtonItem *)sender
{
    if ([FileHandle shareInstance].loginState != YES ) {
        
        LoginViewController *loginVC = [[LoginViewController alloc]init];
        UINavigationController *loginNC = [[UINavigationController alloc]initWithRootViewController:loginVC];
        [self presentViewController:loginNC animated:YES completion:nil];
    }else{
        NSString *uesrName = [FileHandle shareInstance].username;
        UserInformation *user = [[DataBase sharedDataBase] selectInformationWithUserName:uesrName];

        NSMutableArray * movieArray = [NSMutableArray arrayWithArray:user.movieAarry];

        for (MovieDetail *movieDetail in movieArray) {
          if ([movieDetail.movieId isEqualToString:self.movieDetail.movieId]) {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已收藏" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
                [alertView show];
                NSTimer *time = [NSTimer timerWithTimeInterval:1 target:alertView selector:@selector(dismissViewControllerAnimated:completion:) userInfo:nil repeats:NO];
                [alertView dismissWithClickedButtonIndex:1 animated:YES];
                return;
            }
        }
  
        [movieArray addObject:self.movieDetail];
        [[DataBase sharedDataBase] updataUserInformationMovieArray:movieArray activity:user.Activity withUserName:uesrName];
//        UserInformation *user1 = [[DataBase sharedDataBase] selectInformationWithUserName:uesrName];
        [[FileHandle shareInstance] setArrayMovie:movieArray];
        
        [[FileHandle shareInstance] synchronize];
        
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        [alertView show];
        NSTimer *time = [NSTimer timerWithTimeInterval:1 target:alertView selector:@selector(dismissViewControllerAnimated:completion:) userInfo:nil repeats:NO];
        [alertView dismissWithClickedButtonIndex:1 animated:YES];
    }
}





- (void)viewDidDisappear:(BOOL)animated
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)leftBarButtonItemAction:(UIBarButtonItem *)sender
{
    
    [self.navigationController popViewControllerAnimated:YES];

}

+ (CGFloat)heightForLabelText:(NSString *)text
{
    CGSize size = CGSizeMake(350, 1000);
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.height;
}



@end
